package service;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.log.LogService;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;

public class LibraryLogServiceListener implements ServiceTrackerCustomizer {

	// --- Members -------------------------------------------------------------
	LogService log = null;
	private BundleContext bundleContext = null;
	private ServiceTracker logServiceTracker = null;

	// --- Constructors, Getters/Setters ---------------------------------------
	public LibraryLogServiceListener(BundleContext bundleContext) {
		this.bundleContext = bundleContext;
		logServiceTracker = new ServiceTracker(bundleContext,
				LogService.class.getName(), this);
		logServiceTracker.open();
	}

	public LogService getLog() {
		return log;
	}

	// --- Methods -------------------------------------------------------------
	public void stop() {
		logServiceTracker.close();
		log = null;
	}

	// --- Implementation ServiceTrackerCustomizer interface -------------------
	public Object addingService(ServiceReference reference) {
		log = (LogService) bundleContext.getService(reference);
		return log;
	}

	public void modifiedService(ServiceReference reference, Object service) {
	}

	public void removedService(ServiceReference reference, Object service) {
		log = null;
	}

}
